package com.atguigu.gulimail.product.thread;


import java.util.concurrent.*;

public class ThreadTest {
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);


    public static void main(String[] args) throws Exception {
        System.out.println("main方法启动..进程:" + Thread.currentThread().getId());
//        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("当前进程"+Thread.currentThread().getId());
//            Integer i=10/2;
//            System.out.println("计算结果"+i);
//        }, executorService);
//        CompletableFuture<Integer> completableFuture=CompletableFuture.supplyAsync(()->{
//            System.out.println("当前进程"+Thread.currentThread().getId());
//            Integer i=10/0;
//            System.out.println("计算结果"+i);
//            return  i;
//        },executorService).whenComplete((res,exc)->{
//            System.out.println("异步任务完成了。结果是"+res+";异常是:"+exc);
//        }).exceptionally(t->{           //exceptionlly这个是专门处理异常的。如果有异常，默认返回一个数值
//            return 10;
//        });
//        CompletableFuture<Integer> completableFuture=CompletableFuture.supplyAsync(()->{
//            System.out.println("当前进程"+Thread.currentThread().getId());
//            Integer i=10/3;
//            System.out.println("计算结果"+i);
//            return  i;
//        },executorService).handle((res,exc)->{
//            if(res!=null){
//                return res*333;
//            }
//            if(exc!=null){
//                return 0;
//            }
//            return 0;
//
//        });


        /** // thenRun方法    不使用之前的结果，而且没有返回值
         * .thenRunAsync(()->{
         *            System.out.println("任务2启动了");
         *        }, executorService);
         *    //thenAccetpAsync 使用之前的结果。没有返回值
         *  .thenAcceptAsync(res->{
         *            System.out.println("任务2启动了，"+res);
         *        },executorService);
         *
         */
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前进程" + Thread.currentThread().getId());
//            Integer i = 10 / 3;
//            System.out.println("计算结果" + i);
//            return i;
//        }, executorService).thenApplyAsync(res -> {
//            System.out.println("任务2启动了" + res);
//            try {
//                Thread.sleep(2000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            return "hello" + res;
//        }, executorService);

        //两个任务组合 都要完成
        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当任务1开始" + Thread.currentThread().getId());
            Integer i = 10 / 3;
            System.out.println("当任务1结束");
            return i;
        }, executorService);

        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {

            try {
                Thread.sleep(2000);
                System.out.println("当任务2开始" + Thread.currentThread().getId());
                System.out.println("当任务2结束");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "hello";
        }, executorService);


//        future01.runAfterBothAsync(future01, ()->{
//            System.out.println("任务3开始执行");
//        }, executorService);
//        future01.thenAcceptBothAsync(future02, (f1,f2)->{
//            System.out.println("任务3启动："+f1+"--"+f2);
//        }, executorService);
//        CompletableFuture<String> future03 = future01.thenCombineAsync(future02, (f1, f2) -> {
//            return f1 + f2 + "---" + "hh";
//        }, executorService);

//        future01.runAfterEitherAsync(future01, ()->{
//            System.out.println("任务3开始");
//        }, executorService);
//
//        future01.acceptEitherAsync(future02, (t)->{
//            System.out.println("任务3启动了"+t);
//        },executorService);
//
        CompletableFuture<String> future = future01.applyToEitherAsync(future01, (t) -> {
            System.out.println("任务三启动了");
            return t.toString() + "helloworld";
        }, executorService);

        System.out.println("main方法结束.." + "--" + future.get());
    }


    public static void threadTest(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main方法开始");
//        Integer a=null;
//
//
//
//        Callable01 callable01 = new Callable01();
//        FutureTask<Integer> integerFutureTask = new FutureTask<>(callable01);
//        new Thread(integerFutureTask).start();
//
//        Integer integer = integerFutureTask.get();//这是一个阻塞方法

        executorService.execute(new Runnable01());
        System.out.println("main方法结束");


        // ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor();

    }

    public static class Callable01 implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            System.out.println("callable方法开始");
            Integer i = 10 / 2;
            System.out.println("计算结果" + i);
            return i;
        }
    }


    public static class Runnable01 implements Runnable {

        @Override
        public void run() {
            System.out.println("runnable方法开始");
            Integer i = 10 / 2;
            System.out.println("计算结果" + i);
        }
    }
}
